package codeTop;

public class q43 {
    public String multiply(String num1, String num2) {
        int m = num1.length();
        int n = num2.length();
        String res = "0";
        for (int i = m-1; i >= 0; i--) {
            int num = num1.charAt(i)-'0';
            if (num == 0) {
                continue;
            }
            int plus = 0;
            StringBuilder sb = new StringBuilder();
            for (int j = n-1; j >= 0; j--) {
                int anoNum = num2.charAt(j)-'0';
                sb.insert(0,(num*anoNum+plus)%10);
                plus = (num*anoNum+plus)/10;
            }
            if (plus != 0) {
                sb.insert(0,plus);
            }
            if (!sb.toString().equals("0")) {
                sb.append("0".repeat(m-1-i));
            }
            res = plusNum(res,sb.toString());
        }
        return res;
    }

    public String plusNum(String num1,String num2){
        int idx1 = num1.length()-1;
        int idx2 = num2.length()-1;
        int plus = 0;
        StringBuilder sb = new StringBuilder();
        while (idx1 >= 0 || idx2 >= 0) {
            int temp = 0;
            if (idx1 >= 0) {
                temp+=num1.charAt(idx1)-'0';
                idx1--;
            }
            if (idx2 >= 0) {
                temp+=num2.charAt(idx2)-'0';
                idx2--;
            }
            sb.insert(0,(temp+plus)%10);
            plus = (temp+plus)/10;
        }
        if (plus != 0) {
            sb.insert(0,plus);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        q43 q = new q43();
        System.out.println(q.multiply("123", "456"));
    }
}
